package com.kexio.user.dto;

import com.fasterxml.jackson.annotation.JsonInclude;

import java.io.Serializable;

/**
 * 用户统计信息DTO
 * 
 * 用于返回用户各种状态的统计信息
 * 
 * @author Kexio Team
 * @since 1.0.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class UserStatistics implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 总用户数
     */
    private Long totalUsers;
    
    /**
     * 活跃用户数（启用且未锁定）
     */
    private Long activeUsers;
    
    /**
     * 锁定用户数
     */
    private Long lockedUsers;
    
    /**
     * 禁用用户数
     */
    private Long disabledUsers;
    
    /**
     * 本月新增用户数
     */
    private Long newUsersThisMonth;
    
    /**
     * 今日登录用户数
     */
    private Long loginUsersToday;
    
    /**
     * 在线用户数
     */
    private Long onlineUsers;
    
    /**
     * 即将过期密码的用户数（7天内）
     */
    private Long passwordExpireUsers;
    
    /**
     * 从未登录的用户数
     */
    private Long neverLoginUsers;
    
    /**
     * 7天内未登录的用户数
     */
    private Long inactiveUsers;
    
    // ==================== 构造函数 ====================
    
    public UserStatistics() {}
    
    public UserStatistics(Long totalUsers, Long activeUsers, Long lockedUsers, Long disabledUsers) {
        this.totalUsers = totalUsers;
        this.activeUsers = activeUsers;
        this.lockedUsers = lockedUsers;
        this.disabledUsers = disabledUsers;
    }
    
    public UserStatistics(Long totalUsers, Long activeUsers, Long lockedUsers, 
                         Long disabledUsers, Long newUsersThisMonth, Long loginUsersToday) {
        this(totalUsers, activeUsers, lockedUsers, disabledUsers);
        this.newUsersThisMonth = newUsersThisMonth;
        this.loginUsersToday = loginUsersToday;
    }
    
    // ==================== 便捷方法 ====================
    
    /**
     * 获取正常用户数（总用户数 - 禁用用户数 - 删除用户数）
     */
    public Long getNormalUsers() {
        if (totalUsers == null || disabledUsers == null) {
            return null;
        }
        return totalUsers - disabledUsers;
    }
    
    /**
     * 获取活跃用户比例
     */
    public Double getActiveUserRate() {
        if (totalUsers == null || totalUsers == 0 || activeUsers == null) {
            return 0.0;
        }
        return (double) activeUsers / totalUsers * 100;
    }
    
    /**
     * 获取锁定用户比例
     */
    public Double getLockedUserRate() {
        if (totalUsers == null || totalUsers == 0 || lockedUsers == null) {
            return 0.0;
        }
        return (double) lockedUsers / totalUsers * 100;
    }
    
    /**
     * 获取禁用用户比例
     */
    public Double getDisabledUserRate() {
        if (totalUsers == null || totalUsers == 0 || disabledUsers == null) {
            return 0.0;
        }
        return (double) disabledUsers / totalUsers * 100;
    }
    
    /**
     * 判断是否有统计数据
     */
    public boolean hasData() {
        return totalUsers != null && totalUsers > 0;
    }
    
    /**
     * 判断是否有用户活跃度数据
     */
    public boolean hasActivityData() {
        return loginUsersToday != null || onlineUsers != null || inactiveUsers != null;
    }
    
    /**
     * 判断是否有安全相关数据
     */
    public boolean hasSecurityData() {
        return lockedUsers != null || passwordExpireUsers != null;
    }
    
    /**
     * 获取用户健康度评分（0-100）
     */
    public Integer getUserHealthScore() {
        if (!hasData()) {
            return 0;
        }
        
        int score = 100;
        
        // 锁定用户扣分
        if (lockedUsers != null && lockedUsers > 0) {
            score -= Math.min(20, (int) (getLockedUserRate() * 0.2));
        }
        
        // 禁用用户扣分
        if (disabledUsers != null && disabledUsers > 0) {
            score -= Math.min(15, (int) (getDisabledUserRate() * 0.15));
        }
        
        // 密码即将过期用户扣分
        if (passwordExpireUsers != null && passwordExpireUsers > 0 && totalUsers != null) {
            double expireRate = (double) passwordExpireUsers / totalUsers * 100;
            score -= Math.min(10, (int) (expireRate * 0.1));
        }
        
        // 非活跃用户扣分
        if (inactiveUsers != null && inactiveUsers > 0 && totalUsers != null) {
            double inactiveRate = (double) inactiveUsers / totalUsers * 100;
            score -= Math.min(10, (int) (inactiveRate * 0.1));
        }
        
        return Math.max(0, score);
    }
    
    // ==================== Getter/Setter ====================
    
    public Long getTotalUsers() {
        return totalUsers;
    }
    
    public void setTotalUsers(Long totalUsers) {
        this.totalUsers = totalUsers;
    }
    
    public Long getActiveUsers() {
        return activeUsers;
    }
    
    public void setActiveUsers(Long activeUsers) {
        this.activeUsers = activeUsers;
    }
    
    public Long getLockedUsers() {
        return lockedUsers;
    }
    
    public void setLockedUsers(Long lockedUsers) {
        this.lockedUsers = lockedUsers;
    }
    
    public Long getDisabledUsers() {
        return disabledUsers;
    }
    
    public void setDisabledUsers(Long disabledUsers) {
        this.disabledUsers = disabledUsers;
    }
    
    public Long getNewUsersThisMonth() {
        return newUsersThisMonth;
    }
    
    public void setNewUsersThisMonth(Long newUsersThisMonth) {
        this.newUsersThisMonth = newUsersThisMonth;
    }
    
    public Long getLoginUsersToday() {
        return loginUsersToday;
    }
    
    public void setLoginUsersToday(Long loginUsersToday) {
        this.loginUsersToday = loginUsersToday;
    }
    
    public Long getOnlineUsers() {
        return onlineUsers;
    }
    
    public void setOnlineUsers(Long onlineUsers) {
        this.onlineUsers = onlineUsers;
    }
    
    public Long getPasswordExpireUsers() {
        return passwordExpireUsers;
    }
    
    public void setPasswordExpireUsers(Long passwordExpireUsers) {
        this.passwordExpireUsers = passwordExpireUsers;
    }
    
    public Long getNeverLoginUsers() {
        return neverLoginUsers;
    }
    
    public void setNeverLoginUsers(Long neverLoginUsers) {
        this.neverLoginUsers = neverLoginUsers;
    }
    
    public Long getInactiveUsers() {
        return inactiveUsers;
    }
    
    public void setInactiveUsers(Long inactiveUsers) {
        this.inactiveUsers = inactiveUsers;
    }
    
    @Override
    public String toString() {
        return "UserStatistics{" +
                "totalUsers=" + totalUsers +
                ", activeUsers=" + activeUsers +
                ", lockedUsers=" + lockedUsers +
                ", disabledUsers=" + disabledUsers +
                ", newUsersThisMonth=" + newUsersThisMonth +
                ", loginUsersToday=" + loginUsersToday +
                ", onlineUsers=" + onlineUsers +
                ", passwordExpireUsers=" + passwordExpireUsers +
                ", neverLoginUsers=" + neverLoginUsers +
                ", inactiveUsers=" + inactiveUsers +
                '}';
    }
}
